package tcm.com.gistone.api.restController;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import tcm.com.gistone.api.service.WordService;
import tcm.com.gistone.database.config.GetBySqlMapper;
import tcm.com.gistone.util.CaptchaUtil;
import tcm.com.gistone.util.ClientUtil;
import tcm.com.gistone.util.EdatResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangfan on 2017/8/12.
 */
@RestController("apiUserController")
@RequestMapping("api/user")
public class UserController {


    @Autowired
    private GetBySqlMapper getBySqlMapper;
    @Autowired
    WordService wordService;

    /**
     * 获取主题词信息
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "getBookAuthByUserAndBookId", method = RequestMethod.POST)
    public EdatResult getBookAuthByUserAndBookId(HttpServletRequest request, HttpServletResponse response) {
        try {
            ClientUtil.SetCharsetAndHeader(request, response);
            JSONObject data = JSONObject.fromObject(request.getParameter("data"));
            int bookId = data.getInt("bookId");
            int userId = data.getInt("userId");
            String sql = "select * from tb_user_book where book_id=" + bookId + " and user_id=" + userId + " ";
            List<Map> result = getBySqlMapper.findRecords(sql);
            if(result.size()>0){
                return EdatResult.ok(result);
            }else{
                return EdatResult.build(1, "没有购买该书籍");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return EdatResult.build(1, "发生错误");
        }
    }

    @RequestMapping("/yzmimg")
    public void execute(HttpServletRequest request , HttpServletResponse response) throws Exception {
        try {
            // 设置跨域
            request.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            CaptchaUtil.outputCaptcha(request, response);
        } catch (Exception e) {
            System.out.println("UserController 获取验证码异常！");
        }

    }


}
